package copyengine.ui
{
	import flash.display.MovieClip;

	import copyengine.ui.component.button.CEButton;
	import copyengine.ui.component.button.CESelectableButton;
	import copyengine.ui.component.button.animation.ICEButtonAnimation;
	import copyengine.ui.component.button.animation.ICESelectedButtonAnimation;
	import copyengine.ui.component.button.animation.mobile.CEMobileButtonDarkAnimation;
	import copyengine.ui.component.button.animation.mobile.CEMobileSelectedButtonDarkAnimation;
	import copyengine.ui.component.list.CEMList;
	import copyengine.ui.component.list.CEMListCore;
	import copyengine.ui.component.list.CEMListTouchProxy;
	import copyengine.ui.component.list.ICEMListCellRender;
	import copyengine.ui.component.list.impl.CEMListFixedCellRenderCalculateHelper;
	import copyengine.ui.component.progressbar.CEProgressBar;
	import copyengine.ui.component.progressbar.animation.CEMProgressMaskAnimation;
	import copyengine.ui.component.progressbar.animation.ICEProgressBarAnimation;
	import copyengine.ui.component.tabbar.CETabBar;

	/**
	 * 用于Mobile 初始化UI组件
	 */
	public final class CEMComponentUtils
	{
		public function CEMComponentUtils()
		{
		}

		public static function getFixedHorizontalList(_targetMc:MovieClip, _CellRenderClass:Class, _dataProvider:Array):CEMList
		{
			return getFixedList(_targetMc, CEMListCore.LAYOUT_HORIZONTAL, _CellRenderClass, _dataProvider);
		}

		public static function getFixedVerticalList(_targetMc:MovieClip, _CellRenderClass:Class, _dataProvider:Array):CEMList
		{
			return getFixedList(_targetMc, CEMListCore.LAYOUT_VERTICAL, _CellRenderClass, _dataProvider);
		}

		private static function getFixedList(_targetMc:MovieClip, _direction:int, _CellRenderClass:Class, _dataProvider:Array):CEMList
		{
			var totalCellRenderNum:int=0;
			var allCellRenderVector:Vector.<ICEMListCellRender>=new Vector.<ICEMListCellRender>();
			while (_targetMc["c" + totalCellRenderNum] != null)
			{
				var cellRender:ICEMListCellRender=new _CellRenderClass();
				cellRender.setTargetMc(_targetMc["c" + totalCellRenderNum]);
				allCellRenderVector.push(cellRender);
				totalCellRenderNum++
			}

			var listCalculateHelper:CEMListFixedCellRenderCalculateHelper=new CEMListFixedCellRenderCalculateHelper();
			var listCore:CEMListCore=new CEMListCore();
			var listTouchProxy:CEMListTouchProxy=new CEMListTouchProxy();

			listCalculateHelper.initialize(_targetMc, _direction, totalCellRenderNum);
			listCore.initialize(allCellRenderVector, listCalculateHelper, _dataProvider);
			listTouchProxy.initialize(_targetMc, _direction);

			var list:CEMList=new CEMList(_targetMc);
			list.initialize(listCore, listTouchProxy);
			return list;
		}



		/**
		 *<b>使用简单遮罩层的进度条组件</b>
		 *
		 * @param _targetMc																注意!! swf元件中应包含key值为"maskMc"的遮罩层元件,且遮罩元件的缩放点应该放在
		 * 																								进度条的(0,0)位置,因为会以缩放点进行缩放,默认缩放点为元件的中心
		 *
		 * @param _onProgressPercentChangeCallback						当进度条的百分比发生改变时候的回调该函数
		 *
		 * @param _defaultPercent														进度条初始化时候的百分比,本次设置百分比不会触发进度条的回调函数
		 *
		 * @param _fixedNum																精确到小数点后面几位,默认为0及 默认为进度条百分比仅取整,比如13% 14%.. 不会出现13.57%这样的小数
		 * 																								如果需要小数,可以填入fixedNum,比如需要显示13.57%这样的百分比,可以将fixedNum设置为2
		 * @return
		 */
		public static function getMaskProgressbar(_targetMc:MovieClip,
												  _onProgressPercentChangeCallback:Function=null,
												  _defaultPercent:int=0,
												  _fixedNum:int=0):CEProgressBar
		{
			var progressBar:CEProgressBar=new CEProgressBar(_targetMc);
			var animation:ICEProgressBarAnimation=new CEMProgressMaskAnimation();
			animation.setTarget(_targetMc);
			progressBar.setAnimation(animation);
			progressBar.initialize(_onProgressPercentChangeCallback, _defaultPercent, _fixedNum);
			return progressBar;
		}


		/**
		 * <b>所有子按钮都为DarkSelectableButton的Tabbar</b>
		 *
		 * @param _targetMc													Swf中的Mc(其内部包含所有初始化按钮所需要的子Mc)
		 * @param _defaultTab												创建好后默认需要选中的Tab签
		 * @param _btnNameArray											所有子按钮元件的名称(在Swf中定义好的名字)
		 * @param _onTabbarChangeCallBackFunction			点击Tab签时候的回调函数
		 * @return
		 *
		 */
		public static function getDarkTabbar(_targetMc:MovieClip,
											 _defaultTab:String,
											 _btnNameArray:Array,
											 _onTabbarChangeCallBackFunction:Function=null):CETabBar
		{
			var groupBtnVector:Vector.<CESelectableButton>=new Vector.<CESelectableButton>();
			for each (var currentBtnName:String in _btnNameArray)
			{
				var currentDarkButton:CEButton=getDarkSelectableButton(_targetMc[currentBtnName]);
				groupBtnVector.push(currentDarkButton);
			}
			var tabBar:CETabBar=new CETabBar(_targetMc);
			tabBar.initialzie(_defaultTab, groupBtnVector, _onTabbarChangeCallBackFunction);
			return tabBar;
		}


		/**
		 * <b>DarkButton(点击时候调整饱和度)</b>
		 * </br></br>
		 * 1·Swf中元件为单帧,程序会在点击时候调整饱和度,在松开时恢复饱和度
		 * 2·Swf中的元件不可以本身含有滤镜,因为在MouseUp时候会删除Target上面的所有滤镜
		 *
		 *</br></br>
		 * @param _targetMc				Swf中的Mc
		 * @param _labelTextKey		Button上面的文字(多语言Key值)
		 * @param _contrastNum		点击时候需要设置的对比度值(可选)
		 *
		 */
		public static function getDarkButton(_targetMc:MovieClip,
											 _onClickCallBackFunction:Function=null,
											 _labelTextKey:String=null,
											 _contrastNum:int=int.MAX_VALUE):CEButton
		{
			var btn:CEButton=new CEButton(_targetMc, _labelTextKey, _onClickCallBackFunction);
			var animation:ICEButtonAnimation;
			var arg:Object;
			if (_contrastNum != int.MAX_VALUE)
			{
				arg == null ? arg={} : null;
				arg["contrastNum"]=_contrastNum;
			}

			arg != null ? animation=new CEMobileButtonDarkAnimation(arg) : animation=new CEMobileButtonDarkAnimation();
			animation.setTarget(_targetMc);
			btn.setAnimation(animation);

			return btn;
		}

		/**
		 * <b>DarkButton(点击时候调整饱和度)</b>
		 * </br></br>
		 * 1·Swf中元件为单帧,程序会在点击时候调整饱和度,在松开时恢复饱和度
		 * 2·Swf中的元件不可以本身含有滤镜,因为在MouseUp时候会删除Target上面的所有滤镜
		 *
		 *</br></br>
		 * @param _targetMc						Swf中的Mc
		 * @param _onClickCallBackFunction		当按钮被点击后的回调函数
		 * @param _isAutoSelect					当前按钮是否在点击后自动更改选中状态
		 * @param _labelTextKey					Button上面的文字(非多语言Key值!)
		 * @param _contrastNum					点击时候需要设置的对比度值(可选,不填为默认值)
		 *
		 */
		public static function getDarkSelectableButton(_targetMc:MovieClip,
													   _onClickCallBackFunction:Function=null,
													   _isAutoSelect:Boolean=true,
													   _labelTextKey:String=null,
													   _contrastNum:int=int.MAX_VALUE):CESelectableButton
		{
			var btn:CESelectableButton=new CESelectableButton(_targetMc, _labelTextKey, _isAutoSelect, _onClickCallBackFunction);
			var animation:ICESelectedButtonAnimation;
			var arg:Object;
			if (_contrastNum != int.MAX_VALUE)
			{
				arg == null ? arg={} : null;
				arg["contrastNum"]=_contrastNum;
			}

			arg != null ? animation=new CEMobileSelectedButtonDarkAnimation(arg) : animation=new CEMobileSelectedButtonDarkAnimation();
			animation.setTarget(_targetMc);
			//默认停在未选中状态上
			animation.onSelectedChange(false);
			btn.setAnimation(animation);

			return btn;

		}


	}
}
